package com.mousycoder.search.controller;

import com.mousycoder.search.model.NBAPlayer;
import com.mousycoder.search.service.NBAPlayerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * TODO
 *
 * @author mousycoder
 * @version 1.0
 * @date 2022/4/4 5:02 PM
 */
@RestController
@RequestMapping("/nba")
public class NBASearchController {

    @Autowired
    private NBAPlayerService nbaPlayerService;

    @RequestMapping("/importAll")
    public String importAll() throws IOException {
        nbaPlayerService.importAll();
        return "success";
    }

    @RequestMapping("/searchMatch")
    public List<NBAPlayer> searchMatch(@RequestParam("displayNameEn") String displayNameEn) throws IOException {
        return nbaPlayerService.searchMatch("displayNameEn",displayNameEn);
    }

    @RequestMapping("/searchTerm")
    public List<NBAPlayer> searchTerm(@RequestParam(value = "country",required = false) String country,@RequestParam(value = "teamName",required = false) String teamName) throws IOException {
        if (StringUtils.isNoneBlank(country)){
            return nbaPlayerService.searchMatch("country",country);
        }else {
            return nbaPlayerService.searchMatch("teamName",teamName);
        }
    }

    @RequestMapping("/searchPrefix")
    public List<NBAPlayer> searchPrefix(@RequestParam("displayNameEn") String displayNameEn) throws IOException {
        return nbaPlayerService.searchMatchPrefix("displayNameEn",displayNameEn);
    }
}
